'''
/*
 * @Author: wevsmy 
 * @Date: 2017-10-25 14:35:43 
 * @Last Modified by: wevsmy
 * @Last Modified time: 2017-10-25 14:44:47
 */
'''
#多线程全双工模拟聊天室
from socket import *
from threading import Thread
from time import ctime

#接受程序
def RxData():
    while True:
        recvInfo = udpSocket.recvfrom(1024)
        content,destInfo = recvInfo
        print("\r>>%s:%s\r\n<<"%(str(destInfo),content.decode("gb2312")),end="")

#发送程序
def TxData():
    while True:
        content = input("\r<<")
        udpSocket.sendto(content.encode("gb2312"),(destIp,destPort))

udpSocket = None
destIp = ""
destPort = 0

def main():
    global udpSocket
    global destIp
    global destPort

    destIp = input("请输入对方IP:")
    destPort = int(input("请输入对方Port:"))

    udpSocket = socket(AF_INET,SOCK_DGRAM)
    localPort = int(input("请输入本程序运行端口："))
    udpSocket.bind(("",localPort))

    Tx = Thread(target=TxData)
    Tx.start()
    Rx = Thread(target=RxData)
    Rx.start()
    
    Tx.join()
    Rx.join()

if __name__ == '__main__':
    main()
    